/**
 * 
 */
package com.fidelizarte.ejb.persistence.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.collections.CollectionUtils;
//import org.apache.commons.collections.functors.EqualPredicate;
//import org.apache.commons.beanutils.BeanPredicate;
//import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fidelizarte.common.util.SeguridadesException;
import com.fidelizarte.ejb.persistence.dao.MenuDAO;
import com.fidelizarte.ejb.persistence.entities.Menu;

/**
 * @author Jose A
 *
 */
@Stateless
public class MenuDAOImpl extends AbstractFacadeImpl<Menu> implements MenuDAO{
	
	private static final Logger slf4jLogger = LoggerFactory.getLogger(MenuDAOImpl.class);
	
	public MenuDAOImpl () {}
	
	public MenuDAOImpl (EntityManager entityManager) {
		super(entityManager);
	}

	@Override
	public List<Menu> buscarMenuCriterios(Menu menuDTO) throws SeguridadesException {
		slf4jLogger.info("obtenerCompaniaCriterios");
		
		Predicate predicate = null;
		List<Predicate> criteriaList = null;
		
		List<Menu> menuCol = null;
		
		try {
			
			CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
			CriteriaQuery<Menu> criteriaQuery = criteriaBuilder.createQuery(Menu.class);
			
			Root<Menu> fromMenu = criteriaQuery.from(Menu.class);
			criteriaQuery.select(fromMenu);
			
			criteriaList = new ArrayList<Predicate>();
			
			//por nombre 
			if (/*StringUtils.isNotEmpty(*/!menuDTO.getNombreMenu().isEmpty()) {
				Expression<String> nombreMenu = 
						criteriaBuilder.upper(criteriaBuilder.literal(menuDTO.getNombreMenu()));
				predicate = criteriaBuilder.equal(criteriaBuilder.upper(fromMenu.<String>get("nombreMenu")), nombreMenu);
				criteriaList.add(predicate);
			}
			
			//por url
			if (/*StringUtils.isNotEmpty(*/!menuDTO.getUrlMenu().isEmpty()) {
				Expression<String> urlMenu = 
						criteriaBuilder.upper(criteriaBuilder.literal(menuDTO.getUrlMenu()));
				predicate = criteriaBuilder.equal(criteriaBuilder.upper(fromMenu.<String>get("urlMenu")), urlMenu);
				criteriaList.add(predicate);
			}
			
			criteriaQuery.where(criteriaBuilder.and(criteriaList.toArray(new Predicate[0])));
			criteriaQuery.orderBy(criteriaBuilder.asc(fromMenu.get("fechaRegistro")));
			
			TypedQuery<Menu> typedQuery = entityManager.createQuery(criteriaQuery);
			
			menuCol = typedQuery.getResultList();
			
			/*if (CollectionUtils.isNotEmpty(menuCol)) {
				CollectionUtils.select(menuCol, new org.apache.commons.collections.Predicate() {
					@Override
					public boolean evaluate(Object arg0) {
							Menu menu = (Menu) arg0;
						menu.getPerfils().size();
						return true;
					}
				});
				*/
				/**
				   * select's the Person object's from collection of person objects based on arg
				   * @param propertyName - Person's attribute (firstName (or) lastName (or) salary )
				   * @param value - Value to be compared to propertyName
				   */
				
				//if (CollectionUtils.isNotEmpty(menuCol)) {
				//	 BeanPredicate beanPredicate = new BeanPredicate(propertyName, nameEqlPredicate);
					//CollectionUtils.select(menuCol, beanPredicate); 
				//	EqualPredicate nameEqlPredicate = new EqualPredicate(menuDTO.getNombreMenu());
				//	BeanPredicate beanPredicate = new BeanPredicate("nombreMenu", nameEqlPredicate);
					//CollectionUtils.select(menuCol, beanPredicate);
					/*CollectionUtils.select(menuCol, new org.apache.commons.beanutils.BeanPredicate("nombreMenu",){
						@Override
						public boolean evaluate(Object arg0) {
							Menu menu = (Menu) arg0;
						menu.getPerfils().size();
						return true;
					}*/
				//}
				//  void selectObjectsByName(String propertyName, String value) {
				  //  EqualPredicate nameEqlPredicate = new EqualPredicate(value);
				//    BeanPredicate beanPredicate = new BeanPredicate(propertyName, nameEqlPredicate);
				    //Collection<Person> filteredCollection = CollectionUtils.select(personList, beanPredicate);
				    //System.out.println("Below are person object(s) whose " + propertyName + " is " + value);
				   // System.out.println("Matches for entered criteria "
				     //   + CollectionUtils.countMatches(personList, beanPredicate));
				    //for (Person person : filteredCollection) {
				     // System.out.println(person);
				//    }
				//  }
			//}
			
		}catch (Exception e) {
			throw new SeguridadesException(e);
		}
		
		return menuCol;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Menu> obtenerMenusPorIdPerfil(Integer idPerfil) throws SeguridadesException {
		
		slf4jLogger.info("obtenerMenusPorIdPerfil");
		
		List<Menu> menusUsuario = new ArrayList<Menu>();
		
		try {
			
			Query query = entityManager.createQuery("select m from Menu m join m.perfils p where p.idPerfil =?");
			query.setParameter(1, idPerfil);
			
			menusUsuario = query.getResultList();			
		} catch (Exception e) {
			slf4jLogger.info("error al obtenerMenusDeEmpresa {}", e);
			throw new SeguridadesException(e);
		}
		
		return menusUsuario;
	}
}
